#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<string>

using namespace std;


class myString
{
private:
	string mainstr;
	int size;
	void GetNext(string p, int next[])
	{
		int i = 0;
		int j = -1;
		next[0] = -1;

		while (i < p.length() - 1)
		{
			if (j == -1 || p[i] == p[j])
			{
				i++;
				j++;
				next[i] = j;
			}
			else
			{
				j = next[j];
			}
		}


	}
	int KMPFind(string p, int pos, int next[])
	{
		int i = pos;
		int j = 0;

		while (i < size && j < (int)p.length())
		{
			if (j == -1 || mainstr[i] == p[j])
			{
				i++;
				j++;
			}
			else
			{
				j = next[j];
			}
		}

		if (j == p.length())
		{
			return i - j + 1;
		}
		else
		{
			return 0;
		}
	}
public:
	myString();
	~myString();
	void SetVal(string sp);
	int KMPFindSubstr(string p, int pos);
};
myString::myString()
{
	size = 0;
	mainstr = "";
}
myString::~myString()
{
	size = 0;
	mainstr = "";
}
void myString::SetVal(string sp)
{
	mainstr = "";
	mainstr.assign(sp);
	size = mainstr.length();
}
int myString::KMPFindSubstr(string p, int pos)
{
	int i;
	int L = p.length();
	int* next = new int[L];
	GetNext(p, next);
	//for (i = 0; i < L; i++)
	//	cout << next[i] << ' ';
	//cout << endl;
	int v = -1;
	v = KMPFind(p, pos, next);
	delete[]next;
	return v;
}


void test1()
{
	int t;
	cin >> t;
	while (t--)
	{
		myString str;
		string main, pattern, change;
		cin >> main;
		cin >> pattern;
		cin >> change;
		str.SetVal(main);
		cout << main << endl;
		int pos = str.KMPFindSubstr(pattern, 0);
		if (pos)
		{
			main.replace(pos - 1, pattern.length(), change);
		}
		cout << main << endl;
	}
}

string matched_Prefix_Postfix(string str)
{
	int count = 0;
	string tmp("empty");
	//int left = 0, right = str.length() - 1, curl = 0, curr = str.length() - 1;
	//while (curr > 0)
	//{
	//	if (str[left] == str[curr])
	//	{

	//		while()

	//		tmp = str.substr(curr);
	//		curr--;
	//	}
	//	else
	//	{

	//		curr--;
	//	}

	//}
	
	vector<string> vs(str.length());
	vector<string> rvs(str.length());
	for (size_t i = 0; i < str.length() - 1; i++)
	{
		vs[i] = str.substr(0, i + 1);
	}
	for (size_t i = str.length() - 1; i > 0; i--)
	{
		rvs[i] = str.substr(i);
	}
	for (size_t i = 0, j = vs.size() - 1; i < vs.size() - 1; i++, j--)
	{
		if (vs[i] == rvs[j])
		{
			tmp = vs[i];
		}
	}
	return tmp;
}

void test2()
{
	int t;
	cin >> t;
	while (t--)
	{
		string str;
		cin >> str;
		string tmp = matched_Prefix_Postfix(str);
		cout << tmp << endl;
	}
}

void test3()
{

	string s, s1, s2;
	cin >> s;
	for (int len = s.size() / 2; len > 0; len--)
	{
		for (int i = 0; i < s.size() - len; i++)
		{
			s1 = s.substr(i, len);
			s2 = s.substr(len + i);
			if (s2.find(s1) != string::npos)
			{
				cout << len << endl;
				return;
			}
		}
	}
	cout << -1 << endl;
	return;
}

void kmp(string str) 
{
	int k = -1;
	int j = 0;
	int len = str.length();
	int* next = new int[len + 1];
	for (int i = 0; i <= len; i++) 
	{
		next[i] = 0;
	}
	next[0] = -1;
	while (j < len) 
	{
		if (k == -1 || str[k] == str[j]) 
		{
			next[++j] = ++k;
		}
		else 
		{
			k = next[k];
		}
	}
	int t = next[len];
	if (len % (len - t) == 0) 
	{
		if (next[len] == 0) 
		{
			cout << len;
		}
		else 
		{
			cout << 0;
		}
	}
	else 
	{
		cout << len - t - (t % (len - t));
	}
	cout << endl;
}

void test4()
{
	int t;
	cin >> t;
	while (t--) 
	{
		string str;
		cin >> str;
		kmp(str);
	}
}

typedef struct TreeNode
{
	struct TreeNode* left;
	struct TreeNode* right;
	char val;
} TN;

//TN* CreatTree(const string& s, int& i)
//{
//	if (s[i] == '#')
//	{
//		i++;
//		return nullptr;
//	}
//	TN* root = new TN;
//	root->val = s[i++];
//	root->left = CreatTree(s, i);
//	root->right = CreatTree(s, i);
//	return root;
//}

TN* CreatTree(const string& s, int& i)
{
	if (s[i] == '0')
	{
		i++;
		return nullptr;
	}
	TN* root = new TN;
	root->val = s[i++];
	root->left = CreatTree(s, i);
	root->right = CreatTree(s, i);
	return root;
}

void PreOrder(TN* root)
{
	if (root == nullptr)
	{
		return;
	}
	cout << root->val;
	PreOrder(root->left);
	PreOrder(root->right);
}

void InOrder(TN* root)
{
	if (root == nullptr)
	{
		return;
	}
	InOrder(root->left);
	cout << root->val;
	InOrder(root->right);
}

void PostOrder(TN* root)
{
	if (root == nullptr)
	{
		return;
	}
	PostOrder(root->left);
	PostOrder(root->right);
	cout << root->val;
}

void test5()
{
	int t;
	cin >> t;
	while (t--)
	{
		string s;
		cin >> s;
		int i = 0;
		TN* root = CreatTree(s, i);
		PreOrder(root);
		cout << endl;
		InOrder(root);
		cout << endl;
		PostOrder(root);
		cout << endl;

	}
}

int PreOrderFindLeafNum(TN* root, int& i)
{
	if (root == nullptr)
	{
		return 0;
	}
	if (!root->left && !root->right)
	{
		i++;
	}
	PreOrderFindLeafNum(root->left, i);
	PreOrderFindLeafNum(root->right, i);
	return i;
}

void test6()
{
	int t;
	cin >> t;
	while (t--)
	{
		string s;
		cin >> s;
		int i = 0;
		TN* root = CreatTree(s, i);
		i = 0;
		cout << PreOrderFindLeafNum(root, i) << endl;

	}
}

int main()
{
	//test1();
	//test2();
	//int t;
	//cin >> t;
	//while (t--)
	//{
	//	test3();
	//}
	//test4();
	test6();
	return 0;
}